<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES8 对象方法扩展 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES8 对象方法扩展
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/10/23 15:34 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#ES7-11</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<pre class="line-numbers"><code class="language-js">//声明对象
const school = {
    name: &quot;GDUST&quot;,
    campus: ['南城', '松山湖'],
    subjects: ['Java', '网页设计', '软件测试'],
}

//获取对象所有的键
console.log(Object.keys(school));

//返回：[&quot;name&quot;, &quot;campus&quot;, &quot;subjects&quot;]
</code></pre>
<h2><a id="object-values" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Object.values</h2>
<p><code>Object.values()</code>方法返回一个给定对象的所有可枚举属性值的<strong>数组</strong>（即将<code>Object</code>转换为<code>Array</code>）</p>
<pre class="line-numbers"><code class="language-js">//获取对象所有的值
console.log(Object.values(school));

//返回：[&quot;GDUST&quot;, [&quot;南城&quot;, &quot;松山湖&quot;], [&quot;Java&quot;, &quot;网页设计&quot;, &quot;软件测试&quot;]]
</code></pre>
<h2><a id="object-entries" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Object.entries</h2>
<p><code>Object.entries()</code>方法返回一个给定对象自身可遍历属性<code>[key,value]</code>的<strong>数组</strong></p>
<p>可用于创建<code>Map</code></p>
<pre class="line-numbers"><code class="language-js">//返回对象所有的键和值
console.log(Object.entries(school));
/* 返回：
    [
        [&quot;name&quot;, &quot;GDUST&quot;], 
        [&quot;campus&quot;, [&quot;南城&quot;, &quot;松山湖&quot;]], 
        [&quot;subjects&quot;, [&quot;Java&quot;, &quot;网页设计&quot;, &quot;软件测试&quot;]]
    ] */
    
const m = new Map(Object.entries(school));
console.log(m);

/* 返回：
    Map { 
        &quot;name&quot; =&gt; &quot;GDUST&quot;,
        &quot;campus&quot; =&gt; [&quot;南城&quot;, &quot;松山湖&quot;],
        &quot;subjects&quot; =&gt; [&quot;Java&quot;, &quot;网页设计&quot;, &quot;软件测试&quot;]
    } */
</code></pre>
<h2><a id="object-getownpropertydescriptors" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Object.getOwnPropertyDescriptors</h2>
<p><code>Object.getOwnPropertyDescriptors()</code>方法返回指定对象所有自身属性的描述对象</p>
<pre class="line-numbers"><code class="language-js">//获取对象属性的描述对象
console.log(Object.getOwnPropertyDescriptors(school));

/* 返回：
    Object
        campus: { value: [&quot;南城&quot;, &quot;松山湖&quot;], writable: true, enumerable: true, configurable: true }
        name: { value: &quot;GDUST&quot;, writable: true, enumerable: true, configurable: true }
        subjects: { value: [&quot;Java&quot;, &quot;网页设计&quot;, &quot;软件测试&quot;], writable: true, enumerable: true, configurable: true } */
        
//Object.creatr(原型对象,描述对象)
const obj = Object.create(null, {
    name: {
        //属性值
        value: 'GDUST',
        //属性特性
        writable: true,//是否可写
        configurable: true,//是否可删除
        enumerbale: true,//是否可枚举
    }
})
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>